#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

IMGPV="1.4.14.2616"

build:

clean:
	dh_testdir
	dh_testroot
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	# attempt to create proper library softlinks
	install -d debian/tmp/usr/lib
	for lib in gfx_rel_es8.x/*.so; do \
		cp $$lib debian/tmp/usr/lib/$$(echo $$lib | sed "s:gfx_rel_es8.x/::").$(IMGPV) ; \
		ln -s /usr/lib/$$(echo $$lib | sed "s:gfx_rel_es8.x/::").$(IMGPV) \
			debian/tmp/usr/lib/$$(echo $$lib | sed "s:gfx_rel_es8.x/::") ; \
		ln -s /usr/lib/$$(echo $$lib | sed "s:gfx_rel_es8.x/::").$(IMGPV) \
			debian/tmp/usr/lib/$$(echo $$lib | sed "s:gfx_rel_es8.x/::")$$(echo $(IMGPV) | awk -F. '{print "." $$1}') ; \
	done
	# to be compatible with libgles2-mesa
	ln -s /usr/lib/libGLESv2.so.$(IMGPV) debian/tmp/usr/lib/libGLESv2.so.2
	
	# Do the same as the installer
	install -d debian/tmp/usr/lib/arm-linux-gnueabihf
	ln -s /usr/lib/libGLESv2.so.$(IMGPV) debian/tmp/usr/lib/arm-linux-gnueabihf/libGLESv2.so
	ln -s /usr/lib/libEGL.so.$(IMGPV) debian/tmp/usr/lib/arm-linux-gnueabihf/libEGL.so

	# to be compatible with the newer library name (that doesn't include the EGL entry points)
	ln -s /usr/lib/libGLES_CM.so.$(IMGPV) debian/tmp/usr/lib/libGLESv1_CM.so 
	ln -s /usr/lib/libGLES_CM.so.$(IMGPV) debian/tmp/usr/lib/libGLESv1_CM.so.1 

	# bins
	install -d debian/tmp/usr/bin
	cp gfx_rel_es8.x/pvrsrvctl debian/tmp/usr/bin/pvrsrvctl ; 
	cp gfx_rel_es8.x/pvr2d_test debian/tmp/usr/bin/pvr2d_test ; 

	# Headers
	install -d debain/tmp/usr/include/EGL
	install -d debain/tmp/usr/include/GLES2
	install -d debain/tmp/usr/include/KHR
	cp usr/include/EGL/*.h debain/tmp/usr/include/EGL
	cp usr/include/GLES2/*.h debain/tmp/usr/include/GLES2
	cp usr/include/KHR/*.h debain/tmp/usr/include/KHR

	# pkgconfig 
	install -d debian/tmp/usr/lib/arm-linux-gnueabihf/pkgconfig
	cp usr/lib/arm-linux-gnueabihf/pkgconfig/*.pc debian/tmp/usr/lib/arm-linux-gnueabihf/pkgconfig

	# Powervr.ini
	install -d debian/tmp/etc
	cp etc/powervr.ini debian/tmp/etc

	# systemd startup script
	install -d debian/tmp/lib/systemd/system/
	cp lib/systemd/system/pvrsrv.service debian/tmp/lib/systemd/system/
	install -d debian/tmp/lib/udev/rules.d/
	cp lib/udev/rules.d/99-systemd-pvrsrv.rules debian/tmp/lib/udev/rules.d/

	dh_install
	dh_installdebconf

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs -s
	dh_installudev -s --priority=50
	dh_installchangelogs -s
	dh_installinit -s --error-handler=true
	dh_lintian
	dh_strip -plibegl1-sgx-omap3 --dbg-package=libegl1-sgx-omap3-dbg
	dh_strip -plibgles2-sgx-omap3 --dbg-package=libgles2-sgx-omap3-dbg
	dh_strip -popengles-sgx-omap3 --dbg-package=opengles-sgx-omap3-dbg
	dh_link -s
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_shlibdeps
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-indep:

binary: binary-arch binary-indep
.PHONY: build clean install binary-indep binary-arch binary install
